//
//  RHBaseFullScreenVC.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/4/8.
//

#import "RHBaseFullScreenVC.h"

@interface RHBaseFullScreenVC ()

@end

@implementation RHBaseFullScreenVC


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    delegate.allowRotate = 1;
    //NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
    //[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
    
    if(@available(iOS 16.0, *)) {
        [self.navigationController setNeedsUpdateOfSupportedInterfaceOrientations];
        NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
        UIWindowScene *ws = (UIWindowScene *)array[0];
        UIWindowSceneGeometryPreferencesIOS *geometryPreferences = [[UIWindowSceneGeometryPreferencesIOS alloc] init];
        geometryPreferences.interfaceOrientations = UIInterfaceOrientationMaskLandscapeRight;
        [ws requestGeometryUpdateWithPreferences:geometryPreferences errorHandler:^(NSError * _Nonnull error) {
             //业务代码
        }];
    }else{
        NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
        [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    self.isUnPopGes = YES;
    _bgView = [[UIView alloc]init];
    [self.view addSubview:_bgView];
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        CGFloat leftMargin = 10;
        if(kSafe_top>20){
            leftMargin = kSafe_top +5;
        }
        make.left.mas_offset(leftMargin);
        make.right.mas_offset(-10);
        make.top.mas_offset(10);
        make.bottom.mas_offset(-10);
    }];
    self.bgView.backgroundColor = UIColor.whiteColor;
    [self.bgView setHidden:YES];
    
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(handleDeviceOrientationDidChange:)
                                                     name:UIDeviceOrientationDidChangeNotification
                                                   object:nil];
}

- (void)handleDeviceOrientationDidChange:(UIInterfaceOrientation)interfaceOrientation
{
    UIDevice *device = [UIDevice currentDevice] ;
    
    switch (device.orientation) {
        case UIDeviceOrientationFaceUp:
            NSLog(@"屏幕朝上平躺");
            break;
            
        case UIDeviceOrientationFaceDown:
            NSLog(@"屏幕朝下平躺");
            break;
            
        case UIDeviceOrientationUnknown:
            NSLog(@"未知方向");
            break;
            
        case UIDeviceOrientationLandscapeLeft:
            NSLog(@"屏幕向左横置");
            break;
            
        case UIDeviceOrientationLandscapeRight:
            NSLog(@"屏幕向右橫置");
            break;
            
        case UIDeviceOrientationPortrait:
            NSLog(@"屏幕直立");
            break;
            
        case UIDeviceOrientationPortraitUpsideDown:
            NSLog(@"屏幕直立，上下顛倒");
            break;
            
        default:
            NSLog(@"无法辨识");
            break;
    }
}


#pragma mark -  Autorotate 横竖屏控制
//是否可以旋转
- (BOOL)shouldAutorotate{
    return NO;
}

//支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;//上左右
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationLandscapeRight;//上左右
}


#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-implementations"
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                duration:(NSTimeInterval)duration {
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
       toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        [self.bgView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight?(kSafe_top+5):5);
            make.right.mas_offset(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft?(-5-kSafe_top):-5);
            make.top.mas_offset(5);
            make.bottom.mas_offset(0);
        }];
       
    }else{

    }
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    [self.bgView setHidden:NO];
}
#pragma clang diagnostic pop

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(nonnull id<UIViewControllerTransitionCoordinator>)coordinator{
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    
    if(@available(iOS 16.0, *)) {
        NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
        UIWindowScene *scene = [array firstObject];
        if (scene.interfaceOrientation == UIInterfaceOrientationLandscapeLeft||scene.interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
            [self.bgView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(scene.interfaceOrientation == UIInterfaceOrientationLandscapeRight?(kSafe_top):5);
                make.right.mas_offset(scene.interfaceOrientation == UIInterfaceOrientationLandscapeLeft?(-kSafe_top):-5);
                make.top.mas_offset(5);
                make.bottom.mas_offset(-kSafe_bottom);
            }];
        
            [self.bgView setHidden:NO];
        }
    }else{
        [self.bgView setHidden:NO];
    }
}




- (void)dealloc{
    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:UIDeviceOrientationDidChangeNotification
                                                      object:nil];
}
@end
